package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.CustomMapper;
import com.theme.passport.dao.theme.CarouselDao;
import com.theme.passport.entity.theme.CarouselEntity;
import com.theme.passport.service.theme.CarouselService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("carouselService")
public class CarouselServiceImpl extends ServiceImpl<CarouselDao, CarouselEntity> implements CarouselService {

    @Autowired
    private CustomMapper mapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String keyword = (String) params.get("key");
        String type = (String) params.get("type");
        IPage<CarouselEntity> page = this.page(
                new Query<CarouselEntity>().getPage(params),
                new QueryWrapper<CarouselEntity>()
                        .eq(StringUtils.isNotBlank(type), "type", type)
                        .like(StringUtils.isNotBlank(keyword), "theme_name", keyword)
                        .orderByAsc("sort")
        );
        if (type.equals("1")) {
            List<CarouselEntity> carouselEntityList = new ArrayList<>();
            carouselEntityList = page.getRecords();
            for (CarouselEntity entity : carouselEntityList) {
                entity.setXzl(mapper.getOneInteger("select count(id) from tab_download_history where object_id  = " + entity.getObjectId() + " and  type = 1"));
            }
            page.setRecords(carouselEntityList);
        }

        return new PageUtils(page);
    }
}